package com.example.test.启动线程.生产消费;


public class Consumer implements Runnable {

    private Data data;

    public Consumer(Data data) {
        this.data = data;
    }

    @Override
    public void run() {

        while (true) {
            synchronized (data) {
                if (data.getMessage() != null) {
                    System.out.println(Thread.currentThread().getName());
                    System.out.println("消费消息" + data.toString() + "");
                    data.setMessage(null);
                    data.notify();

                    try {
                        data.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }

    }
}
